package com.zhejiangzhengyuan.municipal_way.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.Case;
import com.zhejiangzhengyuan.municipal_common.entity.File;
import com.zhejiangzhengyuan.municipal_common.entity.InspectionEvent;
import com.zhejiangzhengyuan.municipal_common.entity.dto.NewCase;
import com.zhejiangzhengyuan.municipal_common.entity.dto.OneWaySendCase;
import com.zhejiangzhengyuan.municipal_common.entity.dto.ReceiptCaseDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.*;
import com.zhejiangzhengyuan.municipal_common.mapper.CaseMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.CaseManagementService;
import com.zhejiangzhengyuan.municipal_common.service.CaseService;
import com.zhejiangzhengyuan.municipal_common.service.InspectionEventService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author :linju
 * @since on : 2019/12/4
 */
@RestController
@RequestMapping("/way/InspectionCaseMangement")
@Api(tags = "巡检案件管理")
public class InspectionCaseMangementController {
  private String caseType = "巡检案件";
  private final String moduleName = ConstantUtil.MODULE_NAME.WAY;
  @Autowired private CaseManagementService caseManagementService;
  @Autowired private CaseService caseService;
  @Autowired private CaseMapper caseMapper;
  @Autowired private FileMapper fileMapper;
  @Autowired private InspectionEventService inspectionEventService;

  @ApiOperation(value = "一路一档案件管理 查询案件在未派单时的数据 分页查询", response = OneWayCaseNoHandVo.class)
  @GetMapping("/selectInspectionPage")
  public ResponseVo selectInspectionPage(
      @ApiParam("页码") @Param("pageNo") Long pageNo,
      @ApiParam("每页显示条数") @Param("pageSize") Long pageSize,
      String dataOrigin,
      String reason,
      String name,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为14
    pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
    pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
    Page<OneWayCaseNoHandVo> page = new Page<>(pageNo, pageSize);
    Page<OneWayCaseNoHandVo> all =
        caseManagementService.selectInspectionPage(
            page, caseType, dataOrigin, reason, name, startTimestamp, endTimestamp);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation("一路一档案件管理案件派单")
  @PostMapping("/oneWaySendCase")
  public ResponseVo InspectionSendCase(@ModelAttribute OneWaySendCase oneWaySendCase)
      throws IOException {
    return caseManagementService.OneWaySendCase(oneWaySendCase);
  }

  @ApiOperation("一路一档派单，重新派单，案件多文件上传")
  @PostMapping("/oneWayInsertFileResult")
  public ResponseVo oneWayInsertFileResult(long iid, MultipartFile file) throws IOException {
    if (iid <= 0 || file == null) {
      return ResponseVo.CLIENT_PARAM_ERROR;
    }
    return caseManagementService.getInsertFileResult(iid, file, moduleName);
  }

  @ApiOperation("一路一档案件管理案件复位")
  @PostMapping("/oneWayReset")
  public ResponseVo InspectionReset(@RequestBody InspectionEvent inspectionEvent) {
    if (Objects.isNull(inspectionEvent)) {
      return ResponseVo.CLIENT_PARAM_ERROR;
    }
    int i = inspectionEventService.updateInspectionEvent(inspectionEvent);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("一路一档案件管理新建案件")
  @PostMapping("/oneWayNewCase")
  public ResponseVo OneWayNewCase(
      @Validated @ModelAttribute NewCase newCase, HttpServletRequest request) throws IOException {
    long iid = caseManagementService.insertCaseAndInspection(newCase);
    return ResponseVo.successWithData(iid);
  }

  @ApiOperation("一路一档案件管理,导出案件在未派单时的数据")
  @GetMapping(value = "/exportOneWayNoHandle")
  public void exportOneWayNoHandle(HttpServletResponse response) {
    caseManagementService.exportOneWayNoHandle(response, caseType);
  }

  @ApiOperation(value = "一路一档案件管理 查询案件在处理中和新任务时的数据 分页查询", response = OneWayCaseNoHandVo.class)
  @GetMapping("/selectInspectionCaseHandingPage")
  public ResponseVo selectInspectionCaseHandingPage(
      @ApiParam("页码") @Param("pageNo") Long pageNo,
      @ApiParam("每页显示条数") @Param("pageSize") Long pageSize,
      String dataOrigin,
      String reason,
      String name,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为14
    pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
    pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
    Page<OneWayCaseNoHandVo> page = new Page<>(pageNo, pageSize);
    Page<OneWayCaseNoHandVo> all =
        caseManagementService.selectInspectionCaseHandingPage(
            page, caseType, dataOrigin, reason, name, startTimestamp, endTimestamp);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation(value = "一路一档案件管理 查询案件在处理中的数据 分页查询", response = OneWayCaseNoHandVo.class)
  @GetMapping("/selectInspectionCaseOnlyHandingPage")
  public ResponseVo selectInspectionCaseOnlyHandingPage(
      @ApiParam("页码") @Param("pageNo") Long pageNo,
      @ApiParam("每页显示条数") @Param("pageSize") Long pageSize) {
    // pageNo默认为1；size默认为14
    pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
    pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
    Page<OneWayCaseNoHandVo> page = new Page<>(pageNo, pageSize);
    Page<OneWayCaseNoHandVo> all =
        caseManagementService.selectInspectionCaseOnlyHandingPage(page, caseType);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation("一路一档案件回执")
  @PostMapping("/receiptCase")
  public ResponseVo receiptCase(@Validated @ModelAttribute ReceiptCaseDto receiptCaseDto) {
    long i = caseService.receiptCase(receiptCaseDto, moduleName);
    return ResponseVo.successWithData(i);
  }

  @ApiOperation("回执多文件上传")
  @PostMapping("/uploadReceiptToFile")
  public ResponseVo uploadReceiptToFile(long rid, MultipartFile file) throws IOException {
    int i = caseService.uploadReceiptToFile(rid, file, moduleName);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("一路一档从处理中转向已处理")
  @GetMapping("/caseHandingTurnToCaseIsHandled")
  public ResponseVo caseHandingTurnToCaseIsHandled(Long cid) {
    int i = caseService.caseInspectionHandingTurnToCaseIsHandled(cid);
    return ResponseVo.successWithData(i);
  }

  @ApiOperation("一路一档案件回执打回")
  @GetMapping("/receiptBackCase")
  public ResponseVo receiptCase(Long cid) {
    int i = caseService.repeatReceipt(cid);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("手机端新任务接单")
  @GetMapping("/mobileAcceptTest")
  public ResponseVo mobileNewTest(Long cid) {
    int i = caseService.changeStatus(cid);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("手机端新任务接单详情")
  @GetMapping("/mobileAcceptTestDdetails")
  public ResponseVo mobileAcceptTestDdetails(Long cid) {
    Case ca = caseMapper.getById(cid);
    if (ca.getType().equals("巡检案件")){
      AcceptTestDdetails acceptTestDdetails = caseService.mobileAcceptTestDdetails(cid);
      return ResponseVo.successWithData(acceptTestDdetails);
    }
    CaseNoHandle caseNoHandle = caseService.mobileOtherTestDdetails(cid);
    if (caseNoHandle!=null){
      String link = fileMapper.selectByWid(caseNoHandle.getWid()).getLink();
      caseNoHandle.setLink(link);
    }
      return ResponseVo.successWithData(caseNoHandle);
  }

  @ApiOperation("手机端已完成详情")
  @GetMapping("/mobileAcceptIsHandedDdetails")
  public ResponseVo mobileAcceptIsHandedDdetails(Long cid) {
    Case ca = caseMapper.getById(cid);
    if (ca.getType().equals("巡检案件")){
      CaseTrackVo caseTrackVo = caseManagementService.selectOneWayCaseTrack(cid);
      return ResponseVo.successWithData(caseTrackVo);
    }
    CaseTrackVo caseTrackVo = caseManagementService.selectCaseTrack(cid);
    return ResponseVo.successWithData(caseTrackVo);
  }


  @ApiOperation(value = "一路一档案件管理 查询案件在处理中时的数据 案件状态为新任务", response = OneWayCaseNoHandVo.class)
  @GetMapping("/selectInspectionNewTest")
  public ResponseVo selectInspectionNewTest() {
    List<OneWayCaseNoHandVo> all = caseManagementService.selectInspectionNewTest(caseType);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation("一路一档案件管理案件重新派单")
  @PostMapping("/oneWayRepeatSendCase")
  public ResponseVo OneWayRepeatSendCase(@ModelAttribute OneWaySendCase oneWaySendCase)
      throws IOException {
    ResponseVo responseVo = caseManagementService.OneWayRepeatSendCase(oneWaySendCase);
    return responseVo;
  }

  @ApiOperation(value = "一路一档案件管理 案件追踪，需要传入当前案件的 cid", response = CaseTrackVo.class)
  @GetMapping("/selectOneWayCaseTrack")
  public ResponseVo selectOneWayCaseTrack(@Param("id") Long id) {
    CaseTrackVo caseTrackVo = caseManagementService.selectOneWayCaseTrack(id);
    return caseTrackVo == null
        ? ResponseVo.NOT_FOUND_RESOURCES
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, caseTrackVo);
  }

  @ApiOperation("一路一档案件管理,导出案件在处理时的数据")
  @GetMapping(value = "/exportOneWayHandling")
  public void exportOneWayHandling(HttpServletResponse response) {
    caseManagementService.exportOneWayHandling(response, caseType);
  }

  @ApiOperation(value = "一路一档案件管理 查询案件在已经处理的数据 分页查询", response = OneWayCaseNoHandVo.class)
  @GetMapping("/selectInspectionCaseIsHandledPage")
  public ResponseVo selectInspectionCaseIsHandledPage(
      @ApiParam("页码") Long pageNo,
      @ApiParam("每页显示条数") Long pageSize,
      String dataOrigin,
      String reason,
      String name,
      String startTimestamp,
      String endTimestamp) {
    // pageNo默认为1；size默认为14
    pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo;
    pageSize = pageSize == null || pageSize <= 0 ? 15 : pageSize;
    Page<OneWayCaseNoHandVo> page = new Page<>(pageNo, pageSize);
    Page<OneWayCaseNoHandVo> all =
        caseManagementService.selectInspectionCaseIsHandledPage(
            page, caseType, dataOrigin, reason, name, startTimestamp, endTimestamp);
    return ResponseVo.successWithData(all);
  }

  @ApiOperation("一路一档案件管理,导出案件在已处理时的数据")
  @GetMapping(value = "/exportOneWayIsHandled")
  public void exportOneWayIsHandled(HttpServletResponse response) {
    caseManagementService.exportOneWayIsHandled(response, caseType);
  }

  @ApiOperation(
      value = "一路一档案件管理 案件详情，需要传入当前案件的 cid和巡检事件的iid",
      response = OneWayCaseDetailsVo.class)
  @GetMapping("/selectOneWayCaseDetails")
  public ResponseVo selectOneWayCaseDetails(
      @ApiParam("巡检事件ID") @Param("iid") Long iid, @ApiParam("案件ID") @Param("cid") Long cid) {
    OneWayCaseDetailsVo oneWayCaseDetailsVo =
        caseManagementService.selectOneWayCaseDetails(iid, cid);
    return ResponseVo.successWithData(oneWayCaseDetailsVo);
  }
}
